import numpy as py 
import pymysql
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from joblib import dump

class MysqlUtils(object):
    """数据库工具类
    
    """
    def __init__(self):
        self.conn = pymysql.connect(
            host='127.0.0.1',
            user='root',
            passwd='root',
            database='liu',
            port=3306,
            charset='utf8'
        )
    
    def get_data(self):
        cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)
        sql = """
        SELECT order_id, age, user_count, id_no, user_name, phone FROM ( SELECT o.id as order_id, u.id_no, u.user_name, u.phone,
        case
            when length(u.id_no) = 18 THEN YEAR(now()) - cast(substr(u.id_no, 7, 4) as signed) ELSE null end as age,
                (SELECT count(*) FROM ticket_order_user_rel WHERE order_id = o.id) as user_count
        FROM ticket_order o JOIN ticket_order_user_rel u on u.order_id = o.id WHERE u.id_no is not null ) as sbuquery WHERE user_count = 1 and (age < 18 or age >= 60)
        """
        cursor.execute(sql)
        ret = cursor.fetchall()
        df = pd.DataFrame(ret)
        df.to_csv('shiyan6/scenic_data.csv')
        
if __name__ == '__main__':
    mu = MysqlUtils()
    mu.get_data()